package com.dxy.dxyuser.controller;

import com.dxy.dxycommon.VO.AddressVO;
import com.dxy.dxycommon.VO.ResultVO;
import com.dxy.dxycommon.utils.ResultVOUtil;
import com.dxy.dxyuser.DO.UserAddress;
import com.dxy.dxyuser.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * junfeng 2018/10/31
 */
@RestController
@RequestMapping("/address")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addAddress;

    /**
     * 添加地址/更新地址(需要传地址ID)
     *
     * @param addressVO
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/addAndUpdateAddress", produces = "application/json;charset=UTF-8")
    public ResultVO addAddress(@RequestBody @Validated AddressVO addressVO, BindingResult bindingResult) {
        UserAddress address;
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(bindingResult.getFieldError().getDefaultMessage());
        } else {
            address = addAddress.addAddress(addressVO);
        }
        return ResultVOUtil.success(address);
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteAddress", produces = "application/json;charset=UTF-8")
    public ResultVO deleteAddress(Integer id) {
        addAddress.deleteAddress(id);
        return ResultVOUtil.success();
    }

    /**
     * 查询地址
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/listAddress", produces = "application/json;charset=UTF-8")
    public ResultVO listAddress(String userId) {
        List<AddressVO> list = addAddress.listAddress(userId);
        return ResultVOUtil.success(list);
    }


    @RequestMapping(value = "/selectAddress", produces = "application/json;charset=UTF-8")
    public ResultVO selectAddress(Integer id) {
        AddressVO list = addAddress.selectAddress(id);
        return ResultVOUtil.success(list);
    }


}
